﻿using System;
using Microsoft.EntityFrameworkCore;
using System.Linq;
using System.Reflection;
using Microsoft.Extensions.Logging;
using ZSZ.Core.Entities;

namespace ZSZ.EntityFramework
{
    public class ZSZDbContext : DbContext
    {
        //ILog ILogger,
        //private static ILog log = LogManager.GetLogger(typeof(ZSZDbContext));

        public ZSZDbContext(DbContextOptions<ZSZDbContext> options)
            : base(options)
        {
            
        }

        //创建日志工厂
        private static ILoggerFactory Mlogger => new LoggerFactory()
            .AddConsole((categoryName, logLevel) =>
                (logLevel == LogLevel.Information) && (categoryName == DbLoggerCategory.Database.Command.Name));
            

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            base.OnConfiguring(optionsBuilder);
            optionsBuilder.UseLoggerFactory(Mlogger);
        }


        //public ZSZDbContext():base("name=connstr")
        //    //name=conn1表示使用连接字符串中名字为conn1的去连接数据库
        //{
        //    Database.SetInitializer<ZSZDbContext>(null);
        //    this.Database.Log = (sql) => {
        //        log.DebugFormat("EF执行SQL：{0}", sql);
        //    };        
        //}

        public DbSet<AdminUserEntity> AdminUsers { get; set; }
        public DbSet<UserEntity> Users { get; set; }
        public DbSet<CityEntity> Cities { get; set; }
        public DbSet<CommunityEntity> Communities { get; set; }
        public DbSet<PermissionEntity> Permissions { get; set; }
        public DbSet<RegionEntity> Regions { get; set; }
        public DbSet<RoleEntity> Roles { get; set; }
        public DbSet<SettingEntity> Settings { get; set; }
        public DbSet<AttachmentEntity> Attachments { get; set; }
        public DbSet<HouseEntity> Houses { get; set; }
        public DbSet<HouseAppointmentEntity> HouseAppointments { get; set; }
        public DbSet<IdNameEntity> IdNames { get; set; }
        public DbSet<HousePicEntity> HousePics { get; set; }
        public DbSet<AdminLogEntity> AdminUserLogs { get; set; }

        public DbSet<HouseAttachmentEntity> HouseAttachments{ get; set;}

        public DbSet<RolePermissionEntity> RolePermissions { get; set; }


        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            //查找所有FluentAPI配置
            modelBuilder.AddEntityConfigurationsFromAssembly(GetType().Assembly);
            
                       
        }
        
    }

}
